package org.popupcommunication.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.popupcommunication.dao.GroupDAO;
import org.popupcommunication.dao.UserDAO;
import org.popupcommunication.model.User;
import org.popupcommunication.service.UserManager;
import org.popupcommunication.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("utilisateur")
public class UserController {
	@Autowired
	UserManager userService;
	@Autowired
	UserDAO userRepository;
	@Autowired
	GroupDAO groupRepository;

	private static final String form_edit = "users/editerUser";
	private static final String form_edit_avatar = "users/editerAvatar";	
	private static final String db_modif_confirm = "users/confirmationModif";


	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String editForm(Model model) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    User toto = userService.findByEmail(name);		
		model.addAttribute("utilisateur", toto);
		return form_edit;
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String editingUser(HttpServletRequest request,
			@ModelAttribute("utilisateur") @Valid User user,
			BindingResult result) {
		if (result.hasErrors()) {
			System.out.println(result.toString());
			return form_edit;
		}
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    User u = userService.findByEmail(name);	
		u.setFirstName(user.getFirstName());
		u.setLastName(user.getLastName());
		u.setEmail(user.getEmail());
		u.setFonction(user.getFonction());
		u.setInstitution(user.getInstitution());
		u.setTel(user.getTel());
		u.setGsm(user.getGsm());
		userRepository.save(u);

		return db_modif_confirm;
	}

	@RequestMapping(value = "/editAvatar", method = RequestMethod.GET)
	public String editAvatar(HttpServletRequest request,Model model) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    User toto = userService.findByEmail(name);	
	    String path = null;
	    if (toto.getSrcAvatar() != null && toto.getSrcAvatar().isEmpty() == false){
	    	path = request.getContextPath()+"/resources/upload"+FileUtil.USER_AVATAR+"/"+toto.getSrcAvatar();
	    }
	    else{
	    	path = request.getContextPath()+"/resources/upload"+FileUtil.USER_AVATAR+"/no_avatar.jpg";	    	
	    }
	    model.addAttribute("utilisateur", toto);
	    model.addAttribute("avatar", path);
	    model.addAttribute("imgUser",toto.getSrcAvatar());
		return form_edit_avatar;
	}
	@RequestMapping(value = "/saveAvatar")
	public  ModelAndView saveAvatar(HttpServletRequest request, @RequestParam("srcAvatar") String imgUser) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    User toto = userService.findByEmail(name);	
	    //la renommer
		String oldName = imgUser;
		if (oldName != null && !oldName.isEmpty()) {
			String avatarFolder = request
					.getSession()
					.getServletContext()
					.getRealPath(
							"/resources/upload/" + FileUtil.USER_AVATAR);
			String newName = String.valueOf(toto.getIdUser());
			toto.setSrcAvatar(FileUtil.renameFile(avatarFolder, newName,
					oldName));
		}

		userRepository.save(toto);	    
		return new ModelAndView("redirect:/index");
	}	
}
